var DOC_CONFIG_TABLE_ID = "doc_config_table";
var DOC_CONFIG_PATH = "../account/doc/config/";
var DOC_CONFIG_DEFAULT_TXT_ID = "default_content";
var DOC_CONFIG_ADD_BUTTON_ID = "doc_config_add_button";

var txt_default;
var current_type;

function success(result) {
	// 如果请求失败, 显示错误信息
	if (!result.result) {
		alert(result.err_msg);
		return;
	}
	// 刷新表格
	showDocConfig(current_type);
}

// 获取公文设置
function showDocConfig() {

	var type = current_type;

	request_get(
		DOC_CONFIG_PATH + type.path + 'all',
		function (result) {

			// 如果请求失败, 显示错误信息
			if (!result.result) {
				alert(result.err_msg);
				return;
			}

			var data;
			var index;
			var ids = [];

			if (type === SEAL) {
				// 如果获取公文印章数据, 需整理获取的数据, 并计算默认下标
				var seals = result.data.seals;
				var default_id = result.data.default;

				data = [];
				index = -1;
				for (var i = 0; i < seals.length; i++) {
					data[i] = seals[i].name;
					ids[i] = seals[i].id;
					if (i == default_id) index = i;
				}
			} else {
				// 如果为其他类型, 直接获取数据
				data = result.data[type.type + "s"];
				index = result.data.default;
			}

			// 默认设置的文字提示
			txt_default.empty().text(data[index]);

			// 以表格显示
			doc_config_table.initDocConfigTable(DOC_CONFIG_TABLE_ID, type);
			doc_config_table.listData(data, index, ids);
		}
	);
}

// 添加/编辑公文设置
function editDocConfig(indexOrId) {

	var type = current_type;

	// 请求 url
	var url;
	if (typeof indexOrId == "undefined") {
		url = DOC_CONFIG_PATH + type.type;
	} else {
		url = DOC_CONFIG_PATH + type.path + indexOrId;
	}

	// 请求数据
	var data = {};
	if (typeof indexOrId == "undefined") {
		data.opt = "save";
	} else {
		data.opt = "update";
	}

	if (type != SEAL) {

		var content = $("#edit_block_content");
		var content_value = content.val();
		content.val("");

		if (content_value == "") {
			alert("请填写" + type.name);
			return;
		}

		data[type.type] = content_value;

		request_put(url, data, success);

	} else {

		var name = $("#seal_name");						//印章名称
		var image_file = document.getElementById("seal_image");
		var charge = $("#seal_charge");					//负责人

		if (name.val() == "") {
			alert("请填写印章名称");
			return;
		}
		if (image_file.files.length == 0) {
			alert("请选择公文印章图片");
			return;
		}
		if (charge.val() == "") {
			alert("请填写负责人");
			return;
		}

		data.name = name.val();
		data.charge = charge.val();
		data.img = image_file.files[0];

		// 清空表单
		name.val("");
		charge.val("");
		if (image_file.outerHTML) {
			image_file.outerHTML = image_file.outerHTML;
		} else {
			image_file.value = "";
		}

		request_post_upload_file(url, data, success);
	}

}

// 设置默认
function setDefault(indexOrId) {

	var type = current_type;

	var data = {
		opt: "set_as_default"
	};

	request_put(DOC_CONFIG_PATH + type.path + indexOrId, data, success);

}

// 更新添加块窗口 (edit_block)
function updAddBlock() {

	var type = current_type;

	var edit_block_title = $("#edit_block_title");
	edit_block_title.text("添加" + type.name);

	var edit_block_other_part = $("#edit_block_other_part");
	var edit_block_seal_part = $("#edit_block_seal_part");

	if (type != SEAL) {

		edit_block_other_part.css("display", "block");
		edit_block_seal_part.css("display", "none");

		var edit_block_content_label = $("#edit_block_content_label");
		edit_block_content_label.text(type.name);

	} else {
		edit_block_other_part.css("display", "none");
		edit_block_seal_part.css("display", "block");
	}

	// 添加公文设置按钮
	$("#submit_button").unbind("click").click(function () {
		editDocConfig();
	});
}

// 更新编辑公文设置块窗口
function updModifyBlock(indexOrId) {

	var type = current_type;

	// 块窗口标题
	var edit_block_title = $("#edit_block_title");
	edit_block_title.text("编辑" + type.name);

	// 设置块窗口显示部分
	var edit_block_other_part = $("#edit_block_other_part");
	var edit_block_seal_part = $("#edit_block_seal_part");

	if (type !== SEAL) {

		edit_block_other_part.css("display", "block");
		edit_block_seal_part.css("display", "none");

		var edit_block_content_label = $("#edit_block_content_label");
		edit_block_content_label.text(type.name);

	} else {
		edit_block_other_part.css("display", "none");
		edit_block_seal_part.css("display", "block");
	}

	// 添加公文设置按钮
	$("#submit_button").unbind("click").click(function () {
		editDocConfig(indexOrId);
	});

	// 填充编辑前的值
	request_get(DOC_CONFIG_PATH + type.path + indexOrId, updModifyBlockValue)

}

function updModifyBlockValue(result) {

	if (!result.result) {
		alert(result.err_msg);
		return;
	}

	var type = current_type;
	var data = result.data;

	if (type !== SEAL) {

		var content = $("#edit_block_content");
		for (var e in data) {
			content.val(data[e]);
		}

	} else {

		var name = $("#seal_name");						//印章名称
		var img = $("#preview");						//印章图片
		var charge = $("#seal_charge");					//负责人
		name.val(data.name);
		img.attr("src", DOC_CONFIG_PATH + "seal/" + data.id + "/image");
		charge.val(data.charge);
	}
}

// 更新删除公文设置块窗口
function updDeleteBlock(content, indexOrId) {

	var type = current_type;

	// 块窗口标题
	var delete_block_title = $("#delete_block_title");
	delete_block_title.text("删除" + type.name);

	var delete_block_content = $("#delete_block_content");
	var delete_button = $("#delete_button");

	delete_block_content.text(content);

	delete_button.unbind("click").click(function () {
		requet_delete(DOC_CONFIG_PATH + type.path + indexOrId, success);
	});
}

// 选择指定设置公文设置后对设置部分的更改
function selectOption() {

	var type = current_type;

	// 更新添加按钮
	var btn_add = $("#" + DOC_CONFIG_ADD_BUTTON_ID);
	btn_add.text("添加" + type.name);

	// 展示指定的公文设置
	showDocConfig();
}

$(document).ready(function () {
	
	txt_default = $("#" + DOC_CONFIG_DEFAULT_TXT_ID);
	current_type = HEADER;
	selectOption();

	$("a#headers").click(function () {
		current_type = HEADER;
		selectOption();
	});
	$("a#offices").click(function () {
		current_type = OFFICE;
		selectOption();
	});
	$("a#seals").click(function () {
		current_type = SEAL;
		selectOption();
	});
	$("a#fonts").click(function () {
		current_type = FONT;
		selectOption();
	});
	$("a#types").click(function () {
		current_type = TYPE;
		selectOption();
	});

});

/* ACat i lele. */